import unittest
from pygo1963.model.Board import Board
from pygo1963.model.Move import Move
from pygo1963.model.Vertex import Vertex
from pygo1963.model.Constants import WHITE_COLOR, BLACK_COLOR
from pygo1963.goLogic.GoEvaluator import AdvacedGoEvaluator

class GoEvaluatorTest(unittest.TestCase):


    def setUp(self):
        self.board = Board(9)
        self.evaluator = AdvacedGoEvaluator()

    def test_game_over_evaluation(self):
        
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("a2")))
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("b2")))
        
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("a3")))
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("b3")))
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("c2")))
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("c1")))
        
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("a1")))
        
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("b1")))
        
        self.assertEqual(self.evaluator.evaluate(self.board), self.evaluator.MAX_EVALUATION)
    
    def test_liberties_evaluation(self):
            
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("a1")))
        
        self.assertTrue(self.evaluator.calculate_liberties_value(self.board) > 0)
        
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("e6")))
        
        self.assertTrue(self.evaluator.calculate_liberties_value(self.board) < 0)

        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("e5")))
        
        self.assertTrue(self.evaluator.calculate_liberties_value(self.board) > 0)
    
    def test_atari_value(self):
    
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("a1")))
        
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("a2")))
        
        self.assertEqual(self.evaluator.calculate_atari_value(self.board), -self.evaluator.ATARI_VALUE)

    def test_calculate_connectivity(self):
    
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("a1")))
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("d5")))
        
        self.assertEqual(self.evaluator.calculate_connectivity(self.board), self.evaluator.GROUP_VALUE * 2)
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("f5")))
        
        self.assertEqual(self.evaluator.calculate_connectivity(self.board), self.evaluator.GROUP_VALUE)
    
    def test_calculate_territory(self):
        
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("e5")))
        
        self.assertTrue(self.evaluator.calculate_territory(self.board) > 0)
        
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("e4")))
        self.assertEqual(self.evaluator.calculate_territory(self.board), 0)
        
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("c3")))
        
        self.assertTrue(self.evaluator.calculate_territory(self.board) > 0)

    
if __name__ == "__main__":
    unittest.main()